﻿import cgi
import datetime
import wsgiref.handlers
import os
import string
import MyClassModels
import MyClassCheck
import json
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template


##service part

def Service_AddExam(courseNameInput,examNameInput):
    try:
        exam=MyClassModels.ExamModel()
        exam.courseName = courseNameInput
        exam.examName = examNameInput
        exams=MyClassModels.ExamModel.all()
        exams.filter('courseName =',courseNameInput)
        exams.filter('examName =',examNameInput)
        if exams.count()==0:
            exam.put()
            return 'add succeed'
        else:
            return 'same exam'
    except:
        return 'failed'

def Service_SearchExam(courseNameInput,examNameInput):
    try:
        exams=MyClassModels.ExamModel.all()
        if(courseNameInput!='all'):
            exams.filter('courseName =',courseNameInput)
        if(examNameInput!=''):
            exams.filter('examName =',examNameInput)
        returnJson="["
        for exam in exams:
            returnJson+="{'courseName':'"+exam.courseName+"','examName':'"+exam.examName+"'},"
        if(len(returnJson)>1):
            returnJson=returnJson[0:len(returnJson)-1]
        returnJson+="]"
        return returnJson
    except:
        return 'failed'

def Service_DeleteExam(courseNameInput,examNameInput):
    try:
        exams=MyClassModels.ExamModel.all()
        exams.filter('courseName =',courseNameInput)
        exams.filter('examName =',examNameInput)

        for exam in exams:
            exam.delete()
        return 'succeed'
    except:
        return 'failed'

def Service_AddClass(classNameInput):
    try:
        classnew=MyClassModels.ClassModel()
        classnew.className = classNameInput
        classexist=MyClassModels.ClassModel.all()
        classexist.filter('className =',classNameInput)
        if classexist.count()==0:
            classnew.put()
            return 'add succeed'
        else:
            return 'same class'
    except:
        return 'failed'

def Service_SearchClass(classNameInput):
    try:
        classexist=MyClassModels.ClassModel.all()
        classexist.order('-isActiveClass')
        if(classNameInput!=''):
            classexist.filter('className =',classNameInput)
        returnJson="["
        for classone in classexist:
            #returnJson+="{'className':'"+classone.className+"','schoolName':'"+classone.schoolName+"','gradeName':'"+classone.gradeName+"'},"
            returnJson+="{'className':'"+classone.className+"','isActiveClass':'"+classone.isActiveClass+"'},"
        if(len(returnJson)>1):
            returnJson=returnJson[0:len(returnJson)-1]
        returnJson+="]"
        return returnJson
    except:
        return 'failed'

def Service_DeleteClass(classNameInput):
    try:
        classexist=MyClassModels.ClassModel.all()
        classexist.filter('className =',classNameInput)
        for classone in classexist:
            classone.delete();
            return 'succeed';
    except:
        return 'failed'

def Service_ActiveClass(classNameInput):
    try:
        classActive=MyClassModels.ClassModel.all()
        classActive.filter('isActiveClass =','Y')
        for classone1 in classActive:
            classone1.delete();
            classone1new=MyClassModels.ClassModel()
            classone1new.className=classone1.className
            classone1new.isActiveClass='N';
            classone1new.put();
        classexist=MyClassModels.ClassModel.all()

        classexist.filter('className =',classNameInput)
        for classone2 in classexist:
            classone2.delete()
            classone2new=MyClassModels.ClassModel()
            classone2new.className=classone2.className
            classone2new.isActiveClass='Y';
            classone2new.put();
        return 'succeed';
    except:
        return 'failed'

def Service_AddStudent(studentNameInput,studentNumberInput,studentMailInput,studentSexInput,pwdInput):
    try:
        studentnew=MyClassModels.StudentModel()
        studentnew.studentName=studentNameInput
        studentnew.studentNumber=string.atoi(studentNumberInput)
        studentnew.studentMail=studentMailInput
        studentnew.studentSex=studentSexInput
        studentnew.studentPassword=pwdInput
        studentnew.className=MyClassModels.getActiveClass();
        studentexist=MyClassModels.StudentModel.all()
        studentexist.filter('studentNumber =',string.atoi(studentNumberInput))
        if studentexist.count()==0:
            studentnew.put()
            return 'add succeed'
        else:
            return 'same student'
    except:
        return 'failed'

def Service_AddParent(parentMailInput,studentNumberInput,studentNameInput,parentNameInput):
    try:
        parentnew=MyClassModels.ParentModel()
        parentnew.studentName=studentNameInput
        parentnew.parentStudentNumber=string.atoi(studentNumberInput)
        parentnew.parentMail=parentMailInput
        parentnew.parentName=parentNameInput
        parentnew.className=MyClassModels.getActiveClass();
        parentexist=MyClassModels.ParentModel.all()
        parentexist.filter('parentStudentNumber =',string.atoi(studentNumberInput))
        if parentexist.count()==0:
            parentnew.put()
            return 'add succeed'
        else:
            return 'same parent'
    except:
        return 'failed'

def Service_SearchStudent(choiseInput):
    try:
        studentexist=MyClassModels.StudentModel.all()
        studentexist.order('studentNumber')
        if(choiseInput=='current'):
            studentexist.filter('className =',MyClassModels.getActiveClass())
        returnJson="["
        for studentone in studentexist:
            returnJson+="{'className':'"+studentone.className+"','studentName':'"+studentone.studentName+"','studentNumber':'"+studentone.studentNumber.__str__()+"','studentMail':'"+studentone.studentMail+"','studentSex':'"+studentone.studentSex+"'},"
        if(len(returnJson)>1):
            returnJson=returnJson[0:len(returnJson)-1]
        returnJson+="]"
        return returnJson
    except:
        return 'failed'

def Service_DeleteStudent(studentNumberInput):
    try:
        studentexist=MyClassModels.StudentModel.all()
        studentexist.filter('studentNumber =',string.atoi(studentNumberInput))
        for studentone in studentexist:
            studentone.delete();
        return 'delete succeed'
    except:
        return 'failed'

def Service_SearchStudentByNumber(studentNumberInput):
    try:
        studentexist=MyClassModels.StudentModel.all()
        studentexist.filter('studentNumber =',string.atoi(studentNumberInput))
        returnJson="["
        for studentone in studentexist:
            returnJson+="{'className':'"+studentone.className+"','studentName':'"+studentone.studentName+"','studentNumber':'"+studentone.studentNumber.__str__()+"','studentMail':'"+studentone.studentMail+"','studentSex':'"+studentone.studentSex+"'},"
        if(len(returnJson)>1):
            returnJson=returnJson[0:len(returnJson)-1]
        returnJson+="]"
        return returnJson
    except:
        return '[]'

def Service_SearchExamByName(examNameInput,courseNameInput):
    try:
        examexist=MyClassModels.ExamModel.all()
        #return examexist.count();
        examexist.filter('examName =',examNameInput);
        examexist.filter('courseName =',courseNameInput);
        returnJson="["
        for examone in examexist:
            returnJson+="{'examName':'"+examone.examName+"','courseName':'"+examone.courseName+"'},"
        if(len(returnJson)>1):
            returnJson=returnJson[0:len(returnJson)-1]
        returnJson+="]"
        return returnJson;
    except:
        return '[]'

def Service_GetAllStudents():
    try:
        studentexist=MyClassModels.StudentModel.all()
        studentexist.filter('className =',MyClassModels.getActiveClass())
        studentexist.order('studentNumber');
        returnJson="["
        for studentone in studentexist:
            returnJson+="{'studentName':'"+studentone.studentName+"','studentNumber':'"+studentone.studentNumber.__str__()+"','studentMail':'"+studentone.studentMail+"','studentSex':'"+studentone.studentSex+"'},"
        if(len(returnJson)>1):
            returnJson=returnJson[0:len(returnJson)-1]
        returnJson+="]"
        return returnJson
    except:
        return '[]'

def Service_AddExamRecordList(examRecordJsonInput):
    try:
        list =[]
        jsonOb = json.read(examRecordJsonInput)
        #courseAndexam=jsonOb["course"]+jsonOb["exam"]
        for jsonO in jsonOb["records"]:
            examRecord=MyClassModels.ExamRecordModel()
            examRecord.ExamName=jsonOb["exam"]
            examRecord.CourseName=jsonOb["course"]
            examRecord.RecordValue=jsonO["studentRecord"]
            examRecord.StudentNumber=int(jsonO["studentNumber"]);
            examRecord.ClassName=MyClassModels.getActiveClass();
            list.append(examRecord);
        for record in list:
            recordExist=MyClassCheck.Check_ExamRecordExist(record);
            if recordExist!=None:
                recordExist.RecordValue=record.RecordValue;
                recordExist.put();
            else:
                record.put();
        return 'save succeed'
    except:
        return 'failed'

def Service_SearchExamByCourse(courseName):
    try:
        examexist=MyClassModels.ExamModel.all()
        examexist.filter('courseName =',courseName)
        returnJson="["
        for examone in examexist:
            returnJson+="{'examName':'"+examone.examName+"','courseName':'"+examone.courseName+"'},"
        if(len(returnJson)>1):
            returnJson=returnJson[0:len(returnJson)-1]
        returnJson+="]"
        return returnJson;
    except:
        return '[]'

def Service_SearchRecordByExamAndCourse(examNameInput,courseNameInput):
    try:
        currentClass=MyClassModels.getActiveClass();
        records=MyClassModels.ExamRecordModel.all()
        records.filter('ExamName =',examNameInput)
        records.filter('CourseName =',courseNameInput)
        records.filter('ClassName =',currentClass)
        returnJson="["
        for record in records:
            student=MyClassCheck.Check_StudentNameByNumber(record.StudentNumber)
            if student==None:
                name=u"找不到学生";
            else:
                name=student.studentName;
            returnJson+="{'studentName':'"+name+"','studentNumber':'"+str(record.StudentNumber)+"','recordValue':'"+str(record.RecordValue)+"'},"
        if(len(returnJson)>1):
            returnJson=returnJson[0:len(returnJson)-1]
        returnJson+="]"
        return returnJson;
    except:
        return 'failed'


def Service_SearchStudentByNumber(studentNumber):
    try:
        studentexist=MyClassModels.StudentModel.all();
        studentexist.filter('studentNumber =',string.atoi(studentNumber));
        if studentexist.count()>0:
            return studentexist[0].studentPassword
        else:
            return u"找不到学生";
    except:
        return 'failed'

def Service_ResetPasswordByStudentNymber(studentNumber):
    try:
        studentexist=MyClassModels.StudentModel.all();
        studentexist.filter('studentNumber =',string.atoi(studentNumber));
        if studentexist.count()>0:
            for student in studentexist:
                student.studentPassword=str(student.studentNumber);
                student.put();
            return u"重置密码成功"
        else:
            return u"找不到学生";
    except:
        return 'failed'


def Service_UpdateSingleRecord(studentNumber,examName,courseName,newValue):
    try:
        currentClass=MyClassModels.getActiveClass();
        records=MyClassModels.ExamRecordModel.all()
        records.filter('ExamName =',examName)
        records.filter('CourseName =',courseName)
        records.filter('StudentNumber =',string.atoi(studentNumber))
        records.filter('ClassName =',currentClass)
        if records.count()>0:
            for record in records:
                record.RecordValue=str(newValue);
                record.put();
                return "saved"
        else:
            return "no record";
    except:
        return 'failed'

def Login(studentNumber,passWord):
    try:
        studentexist=MyClassModels.StudentModel.all();
        studentexist.filter('studentNumber =',string.atoi(studentNumber));
        if studentexist.count()>1:
            return u'账号异常，有重复学号存在';
        elif studentexist.count()==0:
            return u'找不到该学号学生';
        else:
            if studentexist[0].studentPassword==passWord:
                return 'succeed'+studentexist[0].studentName;
            else:
                return u'密码错误';
    except:
        return 'failed';



